<?php
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Member\Logic;
use Think\Model;
if (!defined('MODULE_CACHE')) define('MODULE_CACHE', DATA_PATH.'caches_model/');
/* 业务逻辑分层 */
class MemberLogic extends Model {

    public function __construct() {
        $this->db = D('Member');
        $this->error = '';
        $this->models = getcache('model', 'commons');
    }

    /* 注册一个新用户 */
    public function register($info) {
        $setting = getcache('setting', 'member');
        /* 1、检查验证码开启状态 */
        if ($setting['enablcodecheck'] && !checkVerify($info['verify'])) {
            $this->error = '验证码不正确';
            return FALSE;            
        }
        /* 注册默认值 */
        $info['encrypt'] = random(6);
        $info['point'] = (int) $setting['defualtpoint'];
        $info['groupid'] = (isset($setting['register_groupid']) && is_numeric($setting['register_groupid']) && $setting['register_groupid'] > 1) ? $setting['register_groupid'] : 2;

        /* 2、校验所属模型表 */
        $tableName = $this->models[$info['modelid']]['tablename'];

        if (empty($tableName)) {
            $this->error = '数据校验失败';
            return FALSE;
        }
       
        require MODULE_CACHE.'member_input.class.php';
        $member_input = new \member_input($info['modelid']);
        $modelinfo = $member_input->get($info['info']);
        if ($member_input->getError()) {
            $this->error = $member_input->getError();
            return FALSE;
        }

        if ((!empty($setting['emailcheck_model']) && in_array($info['modelid'], $setting['emailcheck_model'])) || (!empty($setting['admincheck_model']) && in_array($info['modelid'], $setting['admincheck_model']))) {
            $info['status'] = 0;
        } else {
            $info['status'] = 1;
        }

        $userid = $this->db->update($info);

        if (!$userid) {
            $this->error = $this->db->getError();
            return FALSE;
        }
        $modelinfo['userid'] = $userid;
        M($tableName)->add($modelinfo);
        
        if (!empty($setting['emailcheck_model']) && in_array($info['modelid'], $setting['emailcheck_model']) && $setting['registerverifymessage']) {
            helpers('mail');
            $code = authcode($userid.'|'.md5(C('AUTHKEY')), 'ENCODE');
            $url = U('Member/Index/verify_email', array('code' => $code), '', TRUE);
            $message = $setting['registerverifymessage'];
            $arr = array('{webname}'  => C('webname'),'{username}' => $info['username'], '{email}' => $info['email'], '{url}' => $url,'{siteurl}'  => HTTP_HOST
            );
            $message = str_replace(array_keys($arr), $arr, $message);         
            sendmail($info['email'], L('reg_verify_email'), $message);
        }
        return $userid;
    }

    public function login($info) {
        $setting = getcache('setting', 'member');
        /* 1、检查验证码开启状态 */
        if ($setting['enablcodecheck'] && !checkVerify($info['verify'])) {
            $this->error = '验证码不正确';
            return FALSE;            
        }
        $username = htmlspecialchars(trim($info['username']));
        $password = htmlspecialchars(trim($info['password']));
        $userinfo = $this->db->getByUsername($username);
        if ($userinfo) {
            if ($userinfo['status'] != 1) {
                $this->error = '您的账户尚未通过审核';
                return FALSE;
            } elseif ($userinfo['islock'] == 1) {
                $this->error = '您的账户被系统锁定，禁止登录';
                return FALSE;
            } elseif ($userinfo['password'] != md5(md5($password.$userinfo['encrypt']))) {
                $this->error = '用户名或密码错误';
                return FALSE;
            } else {
                $this->db->update(array('userid' => $userinfo['userid'], 'lastdate' => NOW_TIME, 'lastip' => get_client_ip(), 'loginnum' => 'loginnum + 1'));
                $keeptime = 86400;
                if ($info['readme'] == 1) $keeptime = 86400 * 30;
                cookie('_userid', $userinfo['userid'], $keeptime);
                cookie('_groupid', $userinfo['groupid'], $keeptime);
                return TRUE;
            }
        } else {
            $this->error = '用户名或密码错误';
            return FALSE;            
        }
    }

    public function getError() {
        return $this->error;
    }
}